package com.heima.search.service.impl;
import java.util.Date;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.feign.BehaviorFeign;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {
    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        if (dto==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApBehaviorEntry entry = getEntry(dto);

        if(entry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"行为实体不存在");
        }

        IPage<ApUserSearch> page = new Page(0,5);
        IPage<ApUserSearch> pageResult = page(page, Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entry.getId()).eq(ApUserSearch::getStatus,1).orderByDesc(ApUserSearch::getCreatedTime));

        return ResponseResult.okResult(pageResult.getRecords());
    }

    private ApBehaviorEntry getEntry(UserSearchDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        return behaviorFeign.findByUserIdOrEntryId(user==null?0:user.getId(),dto.getEquipmentId());
    }

    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {
       if (dto==null||dto.getId()==null)
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApBehaviorEntry entry = getEntry(dto);
        if (entry==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"行为实体不存在");
        ApUserSearch apUserSearch = getById(dto.getId());
        update(apUserSearch,Wrappers.<ApUserSearch>lambdaUpdate().eq(ApUserSearch::getId
        ,dto.getId()).eq(ApUserSearch::getEntryId,entry.getId()).set(ApUserSearch::getStatus,0));
        return ResponseResult.okResult();
    }

    @Override
    @Async("taskExecutor")
    public void insert(Integer entryId, String searchWords) {
        ApUserSearch userSearch = getOne(
                Wrappers.<ApUserSearch>lambdaQuery()
                        .eq(ApUserSearch::getEntryId, entryId)
                        .eq(ApUserSearch::getKeyword, searchWords)
        );
        if(userSearch!=null){
            userSearch.setStatus(1);
            userSearch.setCreatedTime(new Date());
            updateById(userSearch);
        }else {
        ApUserSearch apUserSearch = new ApUserSearch();
        apUserSearch.setEntryId(entryId);
        apUserSearch.setKeyword(searchWords);
        apUserSearch.setStatus(1);
        apUserSearch.setCreatedTime(new Date());
        save(apUserSearch);
        }
    }
}